# -- coding: utf-8 --
# 交叉熵测试
import tensorflow as tf
# 每一列代表一个类别
y_ = tf.constant([[1.0, 0.0, 0.0]])
# 每一行代表一个样本数据
y = tf.constant([[0.5, 0.4, 0.1]])

result = y_* tf.log(y)
# reduce_mean函数获取行数n平均交叉熵，此例中n=3，所以cross_entropy的值等于result除以3
cross_entropy = -tf.reduce_mean(y_* tf.log(tf.clip_by_value(y, 1e-10, 1.0)))

sess = tf.Session()
print sess.run(result)
print sess.run(cross_entropy)

sess.close()
